import { Middleware, Context} from "../core"

/**
 * 解析JSON，负责反序列化
 */
export default class JSONParserMiddleware implements Middleware {
  async process(context: Context, next: () => Promise<void>): Promise<void> {
    try {
      const appData: Record<string, any> = JSON.parse(JSON.stringify(context.json))
      const pagesData: any[] = appData.structData.pages
      pagesData.forEach(page => {
        page.content = JSON.parse(page.content)
      })
      appData.structData.pages = pagesData
      context.json = appData
      next()
    } catch (error) {
      throw new Error(`【json解析出错】- ${error}`)
    }
  }
}